package com.dpj.yygh.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dpj.yygh.listener.DictListener;
import com.dpj.yygh.mapper.DictMapper;
import com.dpj.yygh.model.cmn.Dict;
import com.dpj.yygh.service.DictService;
import com.dpj.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
    @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Dict> dicts = baseMapper.selectList(queryWrapper);
        for(Dict dict:dicts){
            boolean isHas=hasChildren(dict);
            dict.setHasChildren(isHas);
        }
        return dicts;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = "数据字典";
        response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        List<Dict> dicts = baseMapper.selectList(null);

        List<DictEeVo> dictEeVoList=new ArrayList<>();
        for(Dict dict:dicts){
            DictEeVo dictEeVo=new DictEeVo();
            BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);
        }
        try {
            EasyExcel.write(response.getOutputStream(),DictEeVo.class).sheet("1").doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //根据dictcode和value查询
    @Override
    public String getDictName(String dictCode, String value) {
     //如果dictNode为空，直接根据value查询
        if(StringUtils.isEmpty(dictCode)){
            QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("value",value);
            Dict dict = baseMapper.selectOne(queryWrapper);
            return dict.getName();
        }else{
            Dict codeDict=this.getDictByDictCode(dictCode);
            Long parent_id=codeDict.getId();
            //根据parent_id和value进行查询
            QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("parent_id",parent_id).eq("value",value);
            Dict dict = baseMapper.selectOne(queryWrapper);
            return dict.getName();
        }



    }
   //根据dictCode获取下级节点
    @Override
    public List<Dict> findByDictCode(String dictCode) {

        Dict dict = this.getDictByDictCode(dictCode);
        if(dict==null){
            return null;
        }

        List<Dict> childData = this.findChildData(dict.getId());
        return childData;
    }

    private Dict getDictByDictCode(String dictCode) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",dictCode);
        Dict codeDict = baseMapper.selectOne(wrapper);
        return codeDict;
    }

    public boolean hasChildren(Dict dict){

        QueryWrapper<Dict> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",dict.getId());
        Integer integer = baseMapper.selectCount(queryWrapper);
        if(integer>0){
            return true;
        }else{
            return false;
        }

    }
}
